package com.study.talk.ui.bind

import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.study.talk.data.model.GroupMessageRecord
import com.study.talk.data.model.MessageRecord
import com.study.talk.data.net.OkHttp
import com.study.talk.di.AppModule

object CommonBindingAdapter {
    @JvmStatic
    @BindingAdapter(value = ["imageUrl", "placeholder"], requireAll = false)
    fun loadImageWithPlaceholder(view: ImageView, imageUrl: String?, placeholder: Drawable?) {
        if (imageUrl.isNullOrEmpty()){
            Glide.with(view.context).load(placeholder).into(view)
        }else{
            Glide.with(view.context).load(AppModule.getWebUrl() + imageUrl).placeholder(placeholder).error(placeholder).into(view)
        }
    }

    @JvmStatic
    @BindingAdapter(value = ["imageUri", "placeholder"], requireAll = false)
    fun loadImageWithPlaceholder(view: ImageView, imageUri: Uri?, placeholder: Drawable?) {
        if (imageUri == null){
            Glide.with(view.context).load(placeholder).into(view)
        }else{
            Glide.with(view.context).load(imageUri).placeholder(placeholder).error(placeholder).into(view)
        }
    }

    @JvmStatic
    @BindingAdapter(value = ["msg","placeholder"], requireAll = false)
    fun loadMsgImageWithPlaceholder(view: ImageView, msg: MessageRecord?, placeholder: Drawable?){
        if (msg?.messageType == 1){
            if (msg.content == null){
                Glide.with(view.context).load(placeholder).into(view)
            }else{
                Glide.with(view.context).load(msg.content).placeholder(placeholder).error(placeholder).into(view)
            }
        }
    }

    @JvmStatic
    @BindingAdapter(value = ["group_msg","placeholder"], requireAll = false)
    fun loadMsgImageWithPlaceholder(view: ImageView, groupMsg: GroupMessageRecord?, placeholder: Drawable?){
        if (groupMsg?.messageType == 1){
            if (groupMsg.content == null){
                Glide.with(view.context).load(placeholder).into(view)
            }else{
                Glide.with(view.context).load(groupMsg.content).placeholder(placeholder).error(placeholder).into(view)
            }
        }
    }
}